package com.meisupic.copyright.service.impl;

import com.meisupic.copyright.domain.bo.SysUser;
import com.meisupic.copyright.domain.po.Users;
import com.meisupic.copyright.repo.UsersRepo;
import com.meisupic.copyright.service.IUserService;
import com.meisupic.copyright.util.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * creation wuxun
 *
 * @version V1.0
 * @Description: TODO
 * @date ${date} ${time}
 */
@Service("userDetailsService")
public class UserServiceImpl implements UserDetailsService,IUserService {

  @Autowired
  UsersRepo usersRepo;

  @Override
  public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
    Users users = usersRepo.findUsersByUserNameIs(s);
    if (users == null) {
      throw new UsernameNotFoundException("用户名错误");
    }
    SysUser sysUser = BeanMapper.map(users, SysUser.class);
    return sysUser;
  }

  @Override
  public Users findById(Long id) {
    return usersRepo.findOne(id);
  }

  @Override
  public Users findUsersByHuaBanUserId(Long huabanUserId) {
    return usersRepo.findUsersByHuabanUserIdIs(huabanUserId);
  }

  @Override
  public Users saveUsers(Users users) {
    return usersRepo.save(users);
  }

  @Override
  public int updateMobile(String mobile, Long id) {
    return usersRepo.updateMobile(mobile,id);
  }
}
